<template>
  <div>
    <!-- 顶部栏 -->
    <van-nav-bar
      title="登录"
      left-text="返回"
      left-arrow
      @click-left="$router.go(-1)"
    />
    <div class="tp-img">
      <van-image
        width="120"
        height="120"
        fit="cover"
        round
        :src="userIn.photo"
      />
      <van-uploader :after-read="afterRead" />
    </div>
    <van-cell-group>
      <van-cell
        title="名称"
        is-link
        :value="userIn.name"
        @click="showxmClick"
      />
      <van-cell
        title="性别"
        is-link
        :value="userIn.gender ? '女' : '男'"
        @click="showpopup = true"
      />
      <van-cell
        title="生日"
        is-link
        :value="userIn.birthday"
        @click="dataPicker = true"
      />
    </van-cell-group>
    <!-- 编辑名称 -->

    <van-dialog
      v-model="showxm"
      title="修改昵称"
      show-cancel-button
      @confirm="editname"
    >
      <van-field ref="nameField" v-model="newname" placeholder="请输入名称" />
    </van-dialog>
    <!-- 编辑性别 -->
    <van-popup v-model="showpopup" position="bottom">
      <van-nav-bar
        title="修改性别"
        left-text="取消"
        @click-left="showpopup = false"
      />
      <van-cell title="男" is-link @click="editgender(0)" />
      <van-cell title="女" is-link @click="editgender(1)" />
    </van-popup>
    <!-- 选择生日 -->
    <van-popup v-model="dataPicker" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="dataPicker = false"
        @confirm="dateBirthday"
      />
    </van-popup>
    <!-- 裁剪头像 -->
    <div class="cropper-box" v-show="showCropper">
      <vueCropper
        autoCropWidth="120"
        autoCropHeight="120"
        ref="cropper"
        :img="img"
        autoCrop
      ></vueCropper>
      <van-button type="primary" @click="confirmCropper">确认</van-button>
      <van-button type="info" @click="showCropper = false">取消</van-button>
    </div>
  </div>
</template>

<script>
// 导入mapState函数辅助
import { mapState } from 'vuex'
//导入接口
import { editxx, editArte } from './user.js'
//导入moment时间转换组件
import moment from 'moment'
//导入vue-cropper图片裁剪
import { VueCropper } from 'vue-cropper'
export default {
  name: 'edit',
  data () {
    return {
      showxm: false,
      newname: '',
      showpopup: false,
      dataPicker: false,
      minDate: new Date(1913, 0, 1),
      maxDate: new Date(9999, 11, 31),
      currentDate: new Date(),
      showCropper: false, //裁剪窗口
      img: '',
      cancelCropper: false
    }
  },
  computed: {
    ...mapState(['userIn'])
  },
  components: {
    VueCropper
  },
  methods: {
    //弹出编辑姓名弹窗
    showxmClick () {
      //弹出输入文本窗
      this.showxm = true
      //姓名
      this.newname = this.userIn.name
      // 定时器
      this.$nextTick(() => {
        this.$refs.nameField.focus()
      })
    },
    //修改姓名
    editname () {
      editxx({ name: this.newname }).then(res => {
        console.log(res)
        //弹窗提示
        this.$toast.success('名称修改成功')
        this.$store.commit('userInof', {
          ...this.userIn, //先传入原始数据
          name: res.data.data.name //使用新的姓名覆盖原始的姓名
        })
      })
    },
    //修改性别
    editgender (gender) {
      editxx({ gender }).then(res => {
        console.log(res)
        //弹窗提示
        this.$toast.success('性别修改成功')
        this.$store.commit('userInof', {
          ...this.userIn, //先传入原始数据
          gender //覆盖上传的原始性别
        })
      })
    },
    //修改生日
    dateBirthday (value) {
      console.log(value)
      // console.log(this.currentDate)
      let birthday = moment(value).format('YYYY-MM-DD')
      editxx({ birthday }).then(res => {
        //弹窗提示
        this.$toast.success('生日修改成功')
        //传入原始数据
        this.$store.commit('userInof', {
          ...this.userIn,
          //用新数据覆盖原始数据
          birthday: res.data.data.birthday
        })
        //关闭弹窗
        this.dataPicker = false
      })
    },
    //修改头像
    afterRead (file) {
      console.log(file)
      //使用formData处理文件数据
      this.img = file.content
      //弹出裁剪框
      this.showCropper = true
    },
    //裁剪确认按钮
    confirmCropper () {
      //fromData+接口提交到远程
      this.$refs.cropper.getCropData(data => {
        // do something
        console.log(data)
        let fd = new FormData()
        fd.append('photo', data)
        editArte(fd).then(res => {
          console.log(res)
          //弹窗提示
          this.$toast.success('头像修改成功')
          //先传入原始数据
          this.$store.commit('userInof', {
            ...this.userIn,
            //用新数据覆盖原始数据
            photo: res.data.data.photo
          })
          //隐藏裁剪框
          this.showCropper = false
        })
      })
    }
  }
}
</script>

<style lang="less">
.tp-img {
  display: flex;
  justify-content: center;
  margin-top: 15px;
  .van-uploader {
    opacity: 0;
    position: absolute;
    left: 50%;
    transform: translateX(-41px);
    width: 120px;
    height: 120px;
    top: 80px;
  }

  .cropper-box {
    width: 500px;
    height: 500px;
    position: fixed;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
  }
  .van-button--primary {
    position: absolute;
    left: 0;
    bottom: 0;
  }
  .van-button--info {
    position: absolute;
    right: 0;
    bottom: 0;
  }
}
</style>
